package com.chunuo.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chunuo.commonbase.exception.GuliException;
import com.chunuo.commonutils.ResultCode;
import com.chunuo.eduservice.entity.EduCourse;
import com.chunuo.eduservice.entity.EduCourseDescription;
import com.chunuo.eduservice.entity.frontvo.CourseFrontVo;
import com.chunuo.eduservice.entity.frontvo.CourseWebVo;
import com.chunuo.eduservice.entity.vo.CoursePublishVo;
import com.chunuo.eduservice.entity.vo.CourseQuery;
import com.chunuo.eduservice.entity.vo.EduCourseVo;
import com.chunuo.eduservice.mapper.EduCourseMapper;
import com.chunuo.eduservice.service.EduChapterService;
import com.chunuo.eduservice.service.EduCourseDescriptionService;
import com.chunuo.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chunuo.eduservice.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-04
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    //课程的简介
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    // 更新课程的数据回响
    @Autowired
    private EduCourseMapper eduCourseMapper;

    //注入小节
    @Autowired
    private EduVideoService eduVideoService;

    // 章节
    @Autowired
    private EduChapterService chapterService;

    /**
     * 保存添加课程
     * @param eduCourseVo
     * @return
     */
    @Override
    public String saves(EduCourseVo eduCourseVo) {
        if(eduCourseVo==null){
            throw new GuliException(ResultCode.ERROR.getCode(),"内容为空");
        }
        //课程基本信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(eduCourseVo,eduCourse);
        this.save(eduCourse);

        //课程的简介
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        BeanUtils.copyProperties(eduCourseVo,eduCourseDescription);
        eduCourseDescription.setId(eduCourse.getId());
        eduCourseDescriptionService.save(eduCourseDescription);

        return eduCourse.getId();
    }

    //获取课程的基本
    @Override
    public EduCourseVo info(String id) {
        EduCourseVo vo = new EduCourseVo();
        EduCourse course = this.getById(id);
        BeanUtils.copyProperties(course,vo);

        EduCourseDescription description = eduCourseDescriptionService.getById(id);
        vo.setDescription(description.getDescription());

        return vo;
    }

    /**
     * 更新课程的操作
     * @param eduCourseVo
     * @return
     */
    @Override
    public String updates(EduCourseVo eduCourseVo) {
        if(eduCourseVo==null){
            throw new GuliException(ResultCode.ERROR.getCode(),"内容为空");
        }
        //课程基本信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(eduCourseVo,eduCourse);
        this.updateById(eduCourse);

        //课程的简介
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        BeanUtils.copyProperties(eduCourseVo,eduCourseDescription);
        eduCourseDescription.setId(eduCourse.getId());
        eduCourseDescriptionService.updateById(eduCourseDescription);

        return eduCourse.getId();
    }

    /**
     * 发布课程信息回响
     * @param id
     * @return
     */
    @Override
    public CoursePublishVo getPublihCourseInfo(String id) {
        CoursePublishVo publihCourseInfo = eduCourseMapper.getPublihCourseInfo(id);
        return publihCourseInfo;
    }

    /**
     * 分页 条件查询
     * @param page
     * @param limit
     * @param query
     * @return
     */
    @Override
    public IPage<EduCourse> findAll(Integer page, Integer limit, CourseQuery query) {
        // 查询条件
        QueryWrapper wrapper = new QueryWrapper<EduCourse>();
        // 分页数据
        Page<EduCourse> iPage = new Page(page,limit);

        String title = query.getTitle();
        String status = query.getStatus();

        if(title!=null&&!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }

        if(status!=null&&!StringUtils.isEmpty(status)){
            wrapper.eq("status",status);
        }

        //根据更新时间倒序
        wrapper.orderByDesc("gmt_modified");

        IPage courseIPage = this.page(iPage, wrapper);

        return courseIPage;
    }

    /**
     * 删除课程
     * @param id
     */
    @Override
    public void delete(String id) {

        //删除小节
        eduVideoService.delete(id);

        //删除章节
        chapterService.delete(id);

        // 删除简介
        eduCourseDescriptionService.removeById(id);

        this.removeById(id);
    }

    /**
     * 前台热门推荐
     * @return
     */
    @Cacheable(value = "course",key = "'popularCourse'")
    @Override
    public List<EduCourse> popularCourse() {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        wrapper.last("limit 8");
        List<EduCourse> courseList = baseMapper.selectList(wrapper);
        return courseList;
    }

    @Override
    public Map<String, Object> pageQueryCourse(Page<EduCourse> pageParam, CourseFrontVo courseFront) {

        QueryWrapper wrapper = new QueryWrapper();

        if(!StringUtils.isEmpty(courseFront.getSubjectParentId())){
            wrapper.eq("subject_parent_id",courseFront.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(courseFront.getSubjectId())) { //二级分类
            wrapper.eq("subject_id",courseFront.getSubjectId());
        }

        if(!StringUtils.isEmpty(courseFront.getBuyCountSort())) { //关注度
            wrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseFront.getGmtCreateSort())) { //最新
            wrapper.orderByDesc("gmt_create");
        }

        if (!StringUtils.isEmpty(courseFront.getPriceSort())) {//价格
            wrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pageParam,wrapper);

        List<EduCourse> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();//下一页
        boolean hasPrevious = pageParam.hasPrevious();//上一页

        //把分页数据获取出来，放到map集合
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        CourseWebVo courseWebVo = baseMapper.getCourseDescript(courseId);
        return courseWebVo;
    }
}
